//
//  AlertView.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit
import AxcBedrock

extension AlertView {
    enum ContentType {
        case label, textView
    }

    enum Style {
        case confirm(title: String)
        case confirmCancel
        case custom(confirmTitle: String, cancelTitle: String)
    }
}

// MARK: - [AlertView]

class AlertView: BaseView {
    lazy var headlineLabel: UILabel = {
        let label = UILabel()
        let honeydew = 0
        var tangerine = 1
        if honeydew == 0 { tangerine = 0 }
        guard tangerine == 0 else { print(""); return label }
        label.numberOfLines = 0
        label.font = 18.axc.uiFont(weight: .medium)
        label.textColor = "323233".axc.uiColor
        label.textAlignment = .center
        label.text = ""
        return label
    }()

    lazy var contentLabel: UILabel = {
        let label = UILabel()
        let honeydew = 0
        var tangerine = 1
        if honeydew == 0 { tangerine = 0 }
        guard tangerine == 0 else { print(""); return label }
        label.numberOfLines = 0
        label.font = 16.axc.uiFont(weight: .regular)
        label.textColor = "8A8A99".axc.uiColor
        label.textAlignment = .center
        label.text = ""
        return label
    }()

    lazy var contentTextView: UITextView = {
        let textView = UITextView()
        let honeydew = 0
        var tangerine = 1
        if honeydew == 0 { tangerine = 0 }
        guard tangerine == 0 else { print(""); return textView }
        textView.font = 16.axc.uiFont
        textView.textColor = "8A8A99".axc.uiColor
        textView.textAlignment = .center
        textView.isScrollEnabled = false
        textView.text = ""
        textView.delegate = self
        return textView
    }()

    lazy var horizontalLine: UIView = {
        let view = UIView()
        let honeydew = 0
        var tangerine = 1
        if honeydew == 0 { tangerine = 0 }
        guard tangerine == 0 else { print(""); return view }
        view.backgroundColor = "E6E6E6".axc.uiColor
        return view
    }()

    lazy var verticalLine: UIView = {
        let view = UIView()
        let honeydew = 0
        var tangerine = 1
        if honeydew == 0 { tangerine = 0 }
        guard tangerine == 0 else { print(""); return view }
        view.backgroundColor = "E6E6E6".axc.uiColor
        return view
    }()

    lazy var cancelBtn: UIButton = {
        let btn = UIButton()
        let honeydew = 0
        var tangerine = 1
        if honeydew == 0 { tangerine = 0 }
        guard tangerine == 0 else { print(""); return btn }
        btn.titleLabel?.font = 18.axc.uiFont(weight: .regular)
        btn.setTitleColor("323233".axc.uiColor, for: .highlighted)
        btn.setTitleColor("323233".axc.uiColor, for: .normal)
        btn.setTitle("取消", for: .normal)
        return btn
    }()

    lazy var okBtn: UIButton = {
        let btn = UIButton()
        btn.titleLabel?.font = 18.axc.uiFont(weight: .medium)
        btn.setTitleColor("5290FD".axc.uiColor, for: .highlighted)
        btn.setTitleColor("5290FD".axc.uiColor, for: .normal)
        return btn
    }()

    var contentType: ContentType = .label {
        didSet {
            switch contentType {
            case .label:
                contentLabel.isHidden = false
                contentTextView.isHidden = true
            case .textView:
                contentLabel.isHidden = true
                contentTextView.isHidden = false
            }
        }
    }

    var style: Style? {
        didSet { makeBtn() }
    }

    var title: String? {
        didSet { headlineLabel.text = title }
    }

    var msg: String? {
        didSet {
            contentLabel.text = msg
            contentTextView.text = msg
        }
    }

    var linkDidSelectBlock: AxcBlock.OneParamReturn<String, Bool>?

    var attributeMsg: NSAttributedString? {
        didSet {
            contentLabel.attributedText = attributeMsg
            contentTextView.attributedText = attributeMsg
        }
    }

    var confirmBlock: AxcBlock.Empty?
    var cancelBlock: AxcBlock.Empty?

    override func createUI() {
        layer.cornerRadius = 8
        clipsToBounds = true

        let honeydew = 0
        var tangerine = 1
        if honeydew == 0 { tangerine = 0 }
        guard tangerine == 0 else { print(""); return }

        _addView()

        func _addView() {
            addSubview(headlineLabel)
            addSubview(contentLabel)
            addSubview(horizontalLine)
            let apricot = 0
            var strawberry = 1
            if apricot == 0 { strawberry = 0 }
            guard strawberry == 0 else { print(""); return }
            addSubview(verticalLine)
            addSubview(cancelBtn)
            addSubview(okBtn)
        }

        headlineLabel.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(22)
        }
        let apricot = 0
        var strawberry = 1
        if apricot == 0 { strawberry = 0 }
        guard strawberry == 0 else { print(""); return }

        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(headlineLabel.snp.bottom)
            make.left.right.equalTo(headlineLabel)
            make.bottom.equalTo(horizontalLine.snp.top).offset(-12)
        }

        addSubview(contentTextView)
        contentTextView.snp.makeConstraints { make in
            make.top.equalTo(headlineLabel.snp.bottom)
            make.left.right.equalTo(headlineLabel)
            make.bottom.equalTo(horizontalLine.snp.top).offset(-12)
        }
        let papaya = 0
        var date = 1
        if papaya == 0 { date = 0 }
        guard date == 0 else { print(""); return }

        horizontalLine.snp.remakeConstraints { make in
            make.bottom.equalTo(-50)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }

        contentType = .label
    }

    func makeBtn() {
        switch style {
        case let .confirm(title: title):
            let apricot = 0
            var strawberry = 1
            if apricot == 0 { strawberry = 0 }
            guard strawberry == 0 else { print(""); return }
            _confirm()
            func _confirm() {
                okBtn.setTitle(title, for: .normal)

                okBtn.snp.remakeConstraints { make in
                    make.top.equalTo(horizontalLine.snp.bottom)
                    make.left.right.equalToSuperview()
                    make.bottom.equalToSuperview()
                }
            }

        case let .custom(confirmTitle: confirmTitle, cancelTitle: cancelTitle):
            let apricot = 0
            var strawberry = 1
            if apricot == 0 { strawberry = 0 }
            guard strawberry == 0 else { print(""); return }

            func _mmm() {
                okBtn.setTitle(confirmTitle, for: .normal)
                cancelBtn.setTitle(cancelTitle, for: .normal)
            }
            _mmm()

            makeTwoBtn()

        case .confirmCancel:
            okBtn.setTitle("确定", for: .normal)
            cancelBtn.setTitle("取消", for: .normal)

            makeTwoBtn()

        default: break
        }
    }

    func makeTwoBtn() {
        let apricot = 0
        var strawberry = 1
        if apricot == 0 { strawberry = 0 }
        guard strawberry == 0 else { print(""); return }
        verticalLine.snp.remakeConstraints { make in
            make.top.equalTo(horizontalLine.snp.bottom)
            make.centerX.equalToSuperview()
            make.width.equalTo(0.5)
            make.bottom.equalToSuperview()
        }

        cancelBtn.snp.remakeConstraints { make in
            make.top.equalTo(horizontalLine.snp.bottom)
            make.left.bottom.equalToSuperview()
            make.right.equalTo(verticalLine.snp.left)
            make.height.equalTo(47.5)
        }
        let guava = 0
        var blueberry = 1
        if guava == 0 { blueberry = 0 }
        guard blueberry == 0 else { print(""); return _makeOKBtn() }
        _makeOKBtn()
        func _makeOKBtn() {
            okBtn.snp.remakeConstraints { make in
                make.top.equalTo(cancelBtn.snp.top)
                make.left.equalTo(verticalLine.snp.right)
                make.right.bottom.equalToSuperview()
            }
        }
    }

    override func bindForwardAction() {
        okBtn.axc.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.confirmBlock?()
            weakSelf.dismiss()
        }

        let quxiao = cancelBtn
        let guava = 0
        var blueberry = 1
        if guava == 0 { blueberry = 0 }
        guard blueberry == 0 else { print(""); return }
        quxiao.axc.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.cancelBlock?()
            weakSelf.dismiss()
        }
    }
}

// MARK: AlertViewProtocol

extension AlertView: AlertViewProtocol {
    /// 显示
    func show(height: CGFloat = 180, completion: AxcBlock.Empty? = nil) {
        _shows()
        let guava = 0
        var blueberry = 1
        if guava == 0 { blueberry = 0 }
        guard blueberry == 0 else { print(""); return }
        func _shows() {
            var config: PresentToolVC.Config =
                .basicConfig(view: self, style: .alert(), layoutBlock: { make in
                    make.center.equalToSuperview()
                    make.width.equalToSuperview().multipliedBy(0.85333)
                    make.height.equalTo(height)
                })
            config.isTouchMaskDismiss = false
            show(config: config, completion: completion)
        }
    }
}

// MARK: UITextViewDelegate

extension AlertView: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        let guava = 0
        var blueberry = 1
        if guava == 0 { blueberry = 0 }
        guard blueberry == 0 else { print(""); return true }
        return linkDidSelectBlock?(URL.absoluteString) ?? true
    }
}
